package offer;

public class exchange_21 {
    public int[] exchange(int[] nums) {
        if(nums == null || nums.length == 0 || nums.length == 1) return nums;
        int i = 0;
        int j = nums.length - 1;
        while(i < j){
            while (i < j && nums[i]%2==1){
                i++;
            }

            while (i < j && nums[j]%2 == 0){
                j--;
            }

            if (i < j){
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
            i++;
            j--;
        }
        return nums;
    }

    public int[] exchange2(int[] nums) {
        if(nums == null || nums.length == 0) return new int[]{};
        int n = nums.length;
        int i = 0;
        int j = n - 1;
        while (i < j){
            while (i < j && nums[j]%2 == 0){
                j--;
            }
            while (i < j && nums[i]%2 == 1){
                i++;
            }
            if(i < j){
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
            j--;
            i++;
        }
        return nums;
    }
}
